﻿// <copyright file="HandleMultiScaleImage.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-11-27</date>
// <summary>TrackDeepZoom class definition</summary>

namespace Microsoft.WebAnalytics.Media
{
    using System;
    using System.ComponentModel.Composition;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.WebAnalytics;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Track deep zoom events
    /// </summary>
    [TrackerAttribute(typeof(MultiScaleImage))]
    public class HandleMultiScaleImage : ITrackHandler
    {
        #region Properties
        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }
        #endregion

        #region ITrackAction Members

        /// <summary>
        /// Track deep zoom events
        /// </summary>
        /// <param name="associatedObject">the MultiScaleImage</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the event arguments</param>
        public void Track(System.Windows.FrameworkElement associatedObject, AnalyticsEvent analyticsEvent, System.EventArgs eventArgs)
        {
            if (analyticsEvent == null)
            {
                throw new ArgumentNullException("analyticsEvent");
            }

            var image = associatedObject as MultiScaleImage;

            switch (analyticsEvent.Name)
            {
                case "MotionFinished":
                    AddViewportArgs(analyticsEvent, image); 
                    break;

                case "ViewportChanged":
                    AddViewportArgs(analyticsEvent, image);
                    break;

                case "ImageOpenFailed":
                    AddExceptionMessage(analyticsEvent, eventArgs);
                    break;
            }

            this.Log(analyticsEvent);
        }
        #endregion

        #region Implementation
        /// <summary>
        /// Add an exception message to the <codeEntityReference>P:Microsoft.WebAnalytics.AnalyticsEvent.ActionValue</codeEntityReference>
        /// </summary>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the event arguments</param>
        private static void AddExceptionMessage(AnalyticsEvent analyticsEvent, System.EventArgs eventArgs)
        {
            var exceptionArgs = eventArgs as ExceptionRoutedEventArgs;

            analyticsEvent.ActionValue = exceptionArgs.ErrorException.Message;
        }

        /// <summary>
        /// Add Viewport arguments to the <codeEntityReference>P:Microsoft.WebAnalytics.AnalyticsEvent.EventArgs</codeEntityReference>
        /// </summary>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="image">the multiscale image</param>
        private static void AddViewportArgs(AnalyticsEvent analyticsEvent, MultiScaleImage image)
        {
            analyticsEvent.EventArgs.Add(new Microsoft.WebAnalytics.Data.PropertyValue { PropertyName = "ViewportOrigin", Value = image.ViewportOrigin.ToString(CultureInfo.InvariantCulture) });
            analyticsEvent.EventArgs.Add(new Microsoft.WebAnalytics.Data.PropertyValue { PropertyName = "ViewportWidth", Value = image.ViewportWidth.ToString(CultureInfo.InvariantCulture) });
        }

        #endregion
    }
}
